package com.example.studentalarm.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.studentalarm.R;
import com.example.studentalarm.adapter.CustomViewPagerAdapter;
import com.example.studentalarm.calendar.CalendarView;
import com.example.studentalarm.calendar.RepayPlanInfo;
import com.example.studentalarm.utils.LogUtil;
import com.example.studentalarm.utils.TimeUtils;
import com.example.studentalarm.utils.Utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class CalenDarDialog extends Dialog implements  CalendarView.CallBack {
    ViewPager viewPager;
    private TextView showYearView, showMonthView;
    private Context mContext;
    private CustomViewPagerAdapter<CalendarView> viewPagerAdapter;
    private CalendarView[] calendarViews;
    private RepayPlanInfo repayPlanInfo;
    private ArrayList<RepayPlanInfo> repayPlanInfos;
    private ItemOnClick itemOnClick;
    private InitDataInf initDataInf;
    private String selectDate;



    public CalenDarDialog(@NonNull Context context,InitDataInf initDataInf) {
        super(context, R.style.fragment_offline_dialog);
        this.mContext = context;
        this.initDataInf = initDataInf;
    }



    public void setOnItemClick(ItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }

    public interface ItemOnClick {
        void onClick(String date);
    }

    public interface InitDataInf{
        String getSelectDate();
        ArrayList<RepayPlanInfo> getRemindData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initSelectDate();
        initRemind();
    }

    private void initData() {
        selectDate = initDataInf.getSelectDate();
        repayPlanInfos = initDataInf.getRemindData();
    }

    private void initView() {
        setContentView(R.layout.calendar_dialog_layout);
        viewPager = findViewById(R.id.view_pager);
        showYearView = findViewById(R.id.show_year_view);
        showMonthView = findViewById(R.id.show_month_view);

        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.TOP);
    }

    // 初始化选中的日期
    private void initSelectDate() {
        repayPlanInfo = new RepayPlanInfo();
        Calendar date = TimeUtils.stringToDate(selectDate,TimeUtils.PATTERN);
        if (date != null){
            repayPlanInfo.setYear(date.get(Calendar.YEAR));
            repayPlanInfo.setMonth(date.get(Calendar.MONTH)+1);
            repayPlanInfo.setDay(date.get(Calendar.DATE));
        }
        setShowDateViewText(repayPlanInfo.year,repayPlanInfo.month);
        createMassCalendarViews(mContext, 5, this);
        viewPagerAdapter = new CustomViewPagerAdapter<CalendarView>(calendarViews);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(498);
        viewPager.setOnPageChangeListener(new CalendarViewPagerLisenter(viewPagerAdapter));
    }


    //绘制提醒日期
    private void initRemind() {
        CalendarView[] mShowViews = viewPagerAdapter.getAllItems();
        mShowViews[viewPager.getCurrentItem() % mShowViews.length].updateMonthDate(repayPlanInfos);
    }

    private void createMassCalendarViews(Context context, int count, CalendarView.CallBack callBack) {
        calendarViews = new CalendarView[count];
        for (int i = 0; i < count; i++) {
            calendarViews[i] = new CalendarView(context, repayPlanInfo, callBack);
        }
    }


    class CalendarViewPagerLisenter implements ViewPager.OnPageChangeListener {

        private SildeDirection mDirection = SildeDirection.NO_SILDE;
        int mCurrIndex = 498;
        private CalendarView[] mShowViews;

        public CalendarViewPagerLisenter(CustomViewPagerAdapter<CalendarView> viewAdapter) {
            super();
            this.mShowViews = viewAdapter.getAllItems();
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageSelected(int arg0) {
            measureDirection(arg0);
            updateCalendarView(arg0);
            initRemind();
        }

        private void updateCalendarView(int arg0) {
            LogUtil.d("updateCalendarView arg0 = " + arg0);
            if (mDirection == SildeDirection.RIGHT) {
                mShowViews[arg0 % mShowViews.length].rightSilde();

            } else if (mDirection == SildeDirection.LEFT) {
                mShowViews[arg0 % mShowViews.length].leftSilde();
            }
            mDirection = SildeDirection.NO_SILDE;
        }

        /**
         * 判断滑动方向
         *
         * @param arg0
         */
        private void measureDirection(int arg0) {
            if (arg0 > mCurrIndex) {
                mDirection = SildeDirection.RIGHT;

            } else if (arg0 < mCurrIndex) {
                mDirection = SildeDirection.LEFT;
            }
            mCurrIndex = arg0;
        }

    }

    enum SildeDirection {
        RIGHT, LEFT, NO_SILDE;
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, @Nullable Menu menu, int deviceId) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public void clickDate(RepayPlanInfo date) {
        selectDate = date.toString();
        clickDateDismiss(date.toString());
    }

    @Override
    public void onMesureCellHeight(float mCellSpace, float mCellwide) {

    }

    @Override
    public void changeDate(RepayPlanInfo date) {
        setShowDateViewText(date.year, date.month);
    }

    //viewpage滑动时显示当前日期
    private void setShowDateViewText(int year, int month) {
        showYearView.setText(year + "");
        if (month < 10) {
            showMonthView.setText("0" + month + "/");
        } else {
            showMonthView.setText(month + "/");
        }
    }


    private void clickDateDismiss(final String date){
        Utils.showDialog(mContext, "您选择的日期是:", date, new Utils.ShowDialogInterface() {
            @Override
            public void setEnsureButton() {
                itemOnClick.onClick(date);
                CalenDarDialog.this.dismiss();
            }

            @Override
            public void setCancleButton() {

            }
        });
    }


}

